package com.zizo.autohome.reply.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.zizo.autohome.reply.bean.AutoHomeReply;
import com.zizo.core.DAO;
import com.zizo.core.Errors;
import com.zizo.core.T;
import com.zizo.util.Util;

public class ReplyDaoOfAutoHome extends DAO {
	/**
	 * @Title:ReplyDaoOfAutoHome
	 * @Description:
	 * @param currConn
	 * @param coreConn
	 */
	public ReplyDaoOfAutoHome(Connection currConn, Connection coreConn) {
		super(currConn, coreConn);
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getIDs
	 * 
	 * @Description:
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#getIDs()
	 */
	@Override
	public List<String> getIDs() {
		List<String> uid = new ArrayList<>();
		Statement st = null;
		ResultSet rs = null;
		// String sql = "SELECT DISTINCT uid FROM ( SELECT DISTINCT
		// qczj_price.price_yhid as uid FROM qczj_price UNION ALL SELECT
		// DISTINCT qczj_rzck.yhid AS uid FROM qczj_rzck UNION ALL SELECT
		// DISTINCT qczj_kb.kb_yhid AS uid FROM qczj_kb) AS B WHERE uid NOT
		// IN(SELECT DISTINCT qczj_yh_hf.hf_yhid AS uid FROM qczj_yh_hf)";
		String sql = "SELECT DISTINCT uid FROM ( SELECT DISTINCT qczj_price.price_yhid as uid FROM qczj_price UNION ALL SELECT DISTINCT qczj_kb.kb_yhid AS uid FROM qczj_kb) AS B WHERE uid NOT IN(SELECT DISTINCT qczj_yh_hf.hf_yhid AS uid FROM qczj_yh_hf)";
		try {
			st = coreConn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				uid.add(rs.getInt("uid") + "");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return uid;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: insertData
	 * 
	 * @Description:
	 * 
	 * @param t
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#insertData(com.zizo.core.T)
	 */
	@Override
	public int insertData(T t) {
		PreparedStatement ps = null;
		AutoHomeReply rb = (AutoHomeReply) t;
		String sql = "INSERT INTO qczj_yh_hf(hf_yhid,hf_clubid,hf_bbsid,hf_bt,hf_nr,hf_hfuid,hf_lc,hf_time,hf_gxsj) VALUES(?,?,?,?,?,?,?,?,?)";
		try {
			ps = currConn.prepareStatement(sql);
			ps.setInt(1, rb.getHf_yhid());
			ps.setInt(2, rb.getHf_clubid());
			ps.setInt(3, rb.getHf_bbsid());
			ps.setString(4, rb.getHf_bt());
			ps.setString(5, rb.getHf_nr());
			ps.setInt(6, rb.getHf_hfuid());
			ps.setInt(7, rb.getHf_lc());
			ps.setDate(8, (Date) Util.getDate(rb.getHf_time()));
			ps.setTimestamp(9, (Timestamp) Util.getTimeHM(Util.getCurrentDateTime()));
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println();
			e.printStackTrace();
		}
		return 0;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: updateTime
	 * 
	 * @Description:
	 * 
	 * @param type
	 * 
	 * @param id
	 * 
	 * @see com.zizo.core.DAOInterface#updateTime(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public void updateTime(String type, String id) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getTables
	 * 
	 * @Description:
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#getTables()
	 */
	@Override
	public List<String> getTables() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: insertError
	 * 
	 * @Description:
	 * 
	 * @param t
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#insertError(com.zizo.core.T)
	 */
	@Override
	public int insertError(T t) {
		PreparedStatement ps = null;
		String sql = "INSERT INTO error(firstType,secondType,content) VALUES(?,?,?)";
		Errors error = (Errors) t;
		try {
			ps = currConn.prepareStatement(sql);
			ps.setInt(1, error.getFirstType());
			ps.setInt(2, error.getSecondType());
			ps.setString(3, error.getContent());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public List<String> getFinishID() {
		Statement st = null;
		ResultSet rs = null;
		List<String> finIDs = new ArrayList<>();
		String sql = "SELECT yhid FROM reply_fin";
		try {
			st = currConn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				finIDs.add(rs.getInt("yhid") + "");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return finIDs;
	}

	public List<String> getErrorUrl() {
		List<String> errors = new ArrayList<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT content FROM error_copy";
		try {
			st = currConn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String url = rs.getString("content");
				errors.add(url);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return errors;
	}
}